﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class DMWEB_Pages_UserControl_PageNum : System.Web.UI.UserControl
{
    GridView AllGv ; //定义全局变量GridView1
    SqlDataSource AllSds;//定义全局变量SqlDataSource1
    protected void Page_Load(object sender, EventArgs e)
    {
        AllGv = (GridView)(((System.Web.UI.Page)(System.Web.HttpContext.Current.Handler)).FindControl(GV.Text)); //获取父页面的控件并附值GridView1
        AllSds = (SqlDataSource)(((System.Web.UI.Page)(System.Web.HttpContext.Current.Handler)).FindControl(DS.Text));//获取父页面的控件并附值SqlDataSource1
        //if (AllGv == null || AllSds == null) return;
        //UPDATE by xuyongbo 2008-5-21
        if (AllGv == null || AllSds == null)
        {     
            return;
        }

        lblWarn.Visible = false;  //提示 不显示
        ToVis.Visible = true;   //分页导航显示 
       
        AllGv.AllowPaging = true;
        DBbind(this.lblCpi.Text); //绑定数据源
        if (int.Parse(this.lblCpi.Text) > int.Parse(this.lblallPage.Text))
            this.lblCpi.Text = this.lblallPage.Text;
        if (!IsPostBack)
        {
            this.TextBox1.Text = this.lblCpi.Text;
        }

        if (lblAllRow.Text.ToString().Equals("0"))
        {
            this.lblWarn.Visible = true;
            this.ToVis.Visible = false;
        }
    }
    /// <summary>
    /// 首页按钮
    /// </summary>
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        lblCpi.Text = "1";
        DBbind(lblCpi.Text);
        this.TextBox1.Text = this.lblCpi.Text;

    }
    /// <summary>
    /// 上一页
    /// </summary>
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        if (int.Parse(lblCpi.Text) > 1)
        {
            lblCpi.Text = (int.Parse(lblCpi.Text) - 1).ToString();
            DBbind(lblCpi.Text);
            this.TextBox1.Text = this.lblCpi.Text;
        }
    }
    /// <summary>
    /// 下一页
    /// </summary>
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        if (int.Parse(lblCpi.Text) < int.Parse(lblallPage.Text))
        {
            lblCpi.Text = (int.Parse(lblCpi.Text) + 1).ToString();
            DBbind(lblCpi.Text);
            this.TextBox1.Text = this.lblCpi.Text;
        }
    }
    /// <summary>
    /// 尾页
    /// </summary>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        lblCpi.Text = lblallPage.Text.Equals("0") ? "1" : lblallPage.Text;
        DBbind(lblCpi.Text);
        this.TextBox1.Text = this.lblCpi.Text;

        this.LbtFirstPage.Enabled = true;
        this.LbtLastPage.Enabled = false;
    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    public void DBbind(string curnum)
    {
        if (curnum.Equals("0"))
            curnum = "1";

        LbtFirstPage.Enabled = true;
        LbtLastPage.Enabled = true;
        LbtPer.Enabled = true;
        LbtNext.Enabled = true;

        AllGv.PageIndex = int.Parse(curnum) - 1;//当前页码    说明:lblCpi.Text的作用是通过它来取得当前页码
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = AllSds.Select(DataSourceSelectArguments.Empty);
        lblAllRow.Text = ps.DataSourceCount.ToString();//获取数据源中的所有的行
        check_txtRowinOnePage();
        AllGv.PageSize = int.Parse(txtRowinOnePage.Text);
        txtRowinOnePage.Text = AllGv.PageSize.ToString();

        //int pagecount;

        //算出总的页数
        if (ps.DataSourceCount % AllGv.PageSize != 0)
        {
            lblallPage.Text = (ps.DataSourceCount / AllGv.PageSize + 1).ToString();//页的总数量
        }
        else
        {
            lblallPage.Text = (ps.DataSourceCount / AllGv.PageSize).ToString();
        }        //绑定到数据源

        //判断是首页 
        if (curnum.Equals("1"))
        {
            this.LbtFirstPage.Enabled = false;
            this.LbtPer.Enabled = false;
        }
        //判断是尾页
        if(lblallPage.Text.ToString().Equals(curnum))
        {
            this.LbtNext.Enabled = false;
            this.LbtLastPage.Enabled = false;
        }

       // AllGv.DataSourceID = DS.Text;
        if (!lblAllRow.Text.Equals("0") && curnum.Equals("1"))
        {
            this.lblCpi.Text = "1";
        }
    }
    protected void txtRowinOnePage_TextChanged(object sender, EventArgs e)
    {
        check_txtRowinOnePage();
        DBbind(this.lblCpi.Text);
    }
    public void check_txtRowinOnePage()//检查输入每页的行数
    {
        if (txtRowinOnePage.Text.Trim().Equals("") || !IsNumber(txtRowinOnePage.Text.Trim()))
        {
            txtRowinOnePage.Text = "20";
            return;
        }
        if (int.Parse(txtRowinOnePage.Text) < 1)
            txtRowinOnePage.Text = "20";
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)//第几页
    {
        if (this.TextBox1.Text == null || !IsNumber(this.TextBox1.Text))
            return;
        if (int.Parse(this.TextBox1.Text) > 0 && int.Parse(this.TextBox1.Text) <= int.Parse(this.lblallPage.Text))
        {
            DBbind(this.TextBox1.Text);
            this.lblCpi.Text = this.TextBox1.Text;
        }
    }

    /**
     * 判断可否转换为正整数
     * 参数：strNumber要进行判断的字符串
     * 返回：是返回true,否返回false
     * */
    public bool IsNumber(String strNumber)
    {
        Regex numberPattern = new Regex("^[0-9]+$");
        return numberPattern.IsMatch(strNumber);
    }

}

